<?php
namespace app\admin\controller;

use app\MyException;
use think\annotation\route\Group;
use think\annotation\Route;
use think\annotation\route\Middleware;
use app\admin\middleware\CheckAdmin;
use think\facade\Db;

/**
 * 项目管理
 * Class Project
 * @package app\admin\controller
 * @Group("project")
 */
class Project extends Base
{
    /**
     * 项目列表
     * @Route("index", method="POST")
     * @Middleware({CheckAdmin::class})
     */
    public function index()
    {
        $page = input('page', 1, 'intval');
        $pagesize = input('psize', 20, 'intval');
        $list = Db::name('project')->order(['id' => 'desc'])->page($page, $pagesize)->select()->all();
        $result['total'] = Db::name('project_records')->count();
        $result['data'] = $list;
        return json_ok($result);
    }

    /**
     * 保存
     * @Route("save", method="POST")
     * @Middleware({CheckAdmin::class})
     */
    public function save()
    {
        $data = input('post.');
        $insertId = Db::name('project')->insertGetId($data);
        if (empty($data['member_list'])) {
            throw new MyException(10012, 'member_list不能为空');
        }
        $member_list = json_decode($data['member_list'],true);
        foreach ($member_list as &$v){
            $v['record_id'] = $insertId;
        }
        Db::name('project_member')->insertAll($member_list);
        if(!$insertId){
            throw new MyException(10012);
        }
        return json_ok([], 10000, '操作成功');
    }

    /**
     * 保存审核信息
     * @Route("saveReviewInfo", method="POST")
     * @Middleware({CheckAdmin::class})
     */
    public function saveReviewInfo()
    {
        $data = input('post.');
        $res = Db::name('project_review')->insert($data);
        if(!$res){
            throw new MyException(10012);
        }
        return json_ok([], 10000, '操作成功');
    }
}